//
//  LXJOriginalWeiboView.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/30.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

/// 原创微博视图
class LXJOriginalWeiboView: UIView {
    
    // MARK: 微博的模型属性,并在控件上显示内容
    var originalWeibo: LXJWeiboViewModel? {
        didSet{
            /// 控件赋值
            /// 头像
            profile_image_url.sd_setImage(with: URL(string: (originalWeibo?.homeWeiboList?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            /// 认证图标
            verified_type.image = originalWeibo?.verifiedTypeImage
            /// 昵称
            screen_name.text = originalWeibo?.homeWeiboList?.user?.screen_name
            /// 会员等级
            mbrank.image = originalWeibo?.mbrankImage
            /// 时间
            created_at.text = originalWeibo?.createdTimeText
            /// 来源
            source.text = originalWeibo?.resourceText
            /// 微博内容
            text.text = originalWeibo?.homeWeiboList?.text
            /// 转发图片视图的位置动态调整
            retweetedWeiboBottomConstraintFitForView(originalWeibo: originalWeibo)
        }
    }
    
    /// 转发图片视图的底部约束
    fileprivate var originalWeiboBottomConstraint: Constraint?
    
    // MARK: 控件的懒加载
    /// 头像
    fileprivate lazy var profile_image_url: UIImageView = {
        let img = UIImageView()
        /// 设置背景颜色
        img.backgroundColor = UIColor.red
        /// 设置圆角
        img.layer.cornerRadius = 20
        img.layer.masksToBounds = true
        /// 缩放方式
        img.contentMode = .scaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    /// 认证类型图标
    fileprivate lazy var verified_type: UIImageView = {
        let img = UIImageView()
        /// 这只图片
        img.image = UIImage(named: "avatar_vip")
        return img
    }()
    
    /// 昵称
    fileprivate lazy var screen_name = UILabel(text: "生无可恋的喵星人", font: 14, textColor: UIColor.black, textAlignment: .center)
    
    /// 会员等级
    fileprivate lazy var mbrank: UIImageView = {
        let img = UIImageView()
        /// 这只图片
        img.image = UIImage(named: "common_icon_membership")
        return img
    }()
    
    /// 微博发表时间
    fileprivate lazy var created_at = UILabel(text: "昨天 21:45", font: 11, textColor: UIColor.darkGray, textAlignment: .center)
    
    /// 微博发表来源
    fileprivate lazy var source = UILabel(text: "来自 iPhone 7 Plus", font: 11, textColor: UIColor.darkGray, textAlignment: .center)
    
    /// 原创微博内容
    fileprivate lazy var text = UILabel(text: "朝鲜是前世界最厉害的国家,朝鲜是前世界最厉害的国家,朝鲜是前世界最厉害的国家,朝鲜是前世界最厉害的国家,朝鲜是前世界最厉害的国家,朝鲜是前世界最厉害的国家,朝鲜是前世界最厉害的国家", font: 13, textColor: UIColor.black, textAlignment: .left)
    
    /// 原创微博的图片
    fileprivate lazy var pic_urlsView: LXJWeiboMultyPhotosView = LXJWeiboMultyPhotosView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 设置背景颜色
        backgroundColor = UIColor.white
        
        /// 添加到父视图
        addSubview(profile_image_url)
        addSubview(verified_type)
        addSubview(screen_name)
        addSubview(mbrank)
        addSubview(created_at)
        addSubview(source)
        addSubview(text)
        addSubview(pic_urlsView)
        
        /// 自动布局
        profile_image_url.snp.makeConstraints { (make) in
            make.top.leading.equalTo(homeWeiboCellmargin)
            make.size.equalTo(CGSize(width: 40, height: 40)).priority(ConstraintPriority.high)
        }
        
        verified_type.snp.makeConstraints { (make) in
            make.centerX.equalTo(profile_image_url.snp.right).offset(-homeWeiboCellmargin / 2)
            make.centerY.equalTo(profile_image_url.snp.bottom).offset(-homeWeiboCellmargin / 2)
        }
        
        screen_name.snp.makeConstraints { (make) in
            make.top.equalTo(profile_image_url)
            make.leading.equalTo(profile_image_url.snp.trailing).offset(homeWeiboCellmargin)
        }
        
        mbrank.snp.makeConstraints { (make) in
            make.top.equalTo(screen_name)
            make.leading.equalTo(screen_name.snp.trailing).offset(homeWeiboCellmargin / 2)
        }
        
        created_at.snp.makeConstraints { (make) in
            make.bottom.equalTo(profile_image_url)
            make.leading.equalTo(screen_name)
        }
        
        source.snp.makeConstraints { (make) in
            make.bottom.equalTo(created_at)
            make.leading.equalTo(created_at.snp.trailing).offset(homeWeiboCellmargin)
        }
        
        text.snp.makeConstraints { (make) in
            make.top.equalTo(profile_image_url.snp.bottom).offset(homeWeiboCellmargin)
            make.leading.equalTo(self).offset(homeWeiboCellmargin)
            make.trailing.equalTo(self).offset(-homeWeiboCellmargin)
        }
        
        pic_urlsView.snp.makeConstraints { (make) in
            make.top.equalTo(text.snp.bottom).offset(homeWeiboCellmargin)
            make.leading.equalTo(text)
            // MARK: 暂时存在约束冲突问题 已解决
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
        /// 根据原创内容自动匹配大小
        self.snp.makeConstraints { (make) in
            originalWeiboBottomConstraint = make.bottom.equalTo(pic_urlsView).offset(homeWeiboCellmargin).constraint
        }
    }
    
    /// 根据原创内容自动自动调整大小
    fileprivate func retweetedWeiboBottomConstraintFitForView(originalWeibo: LXJWeiboViewModel?) -> Void {
        /// 卸载原来的约束条件
        originalWeiboBottomConstraint?.deactivate()
        
        /// 有转发图片
        if let pic_urls = originalWeibo?.homeWeiboList?.pic_urls, pic_urls.count > 0 {
            /// 显示转发微博的图片
            pic_urlsView.isHidden = false
            /// 控件赋值
            pic_urlsView.pic_urls = pic_urls
            /// 添加约束
            self.snp.makeConstraints { (make) in
                originalWeiboBottomConstraint = make.bottom.equalTo(pic_urlsView).offset(homeWeiboCellmargin).constraint
            }
        }else{/// 没有转发的图片
            /// 隐藏转发微博
            pic_urlsView.isHidden = true
            /// 添加约束
            self.snp.makeConstraints { (make) in
                originalWeiboBottomConstraint = make.bottom.equalTo(text).offset(homeWeiboCellmargin).constraint
            }
        }
    }
}
